/**
 * Created by Administrator on 15-7-16.
 */
var dataCore = new Core();
var LandDialog = RichBase.extend({
	loadingImg : "http://www.12301.cc/images/img/loading.gif",
	EVENTS : {

	},
	currentTitle : "",
	setCurrentTitle : function(title){
		this.currentTitle = title;
	},
	setCurrentLid : function(lid){
		this.lid = lid;
	},
	setCurrentSalerid : function(salerid){
		this.salerid = salerid;
	},
	init : function(){
		var self = this;
		var dialog = this.dialog = new PFT.Dialog({
			title : "设置终端共享",
			onBeforeOpen : function(){
				$("#landMegreName").val(self.currentTitle);
				$("#landSearchInp").val("");
				$("#searchLoading").hide();
				$("#landUl").find(".list").html('<li style="height:150px; line-height:150px; padding-left:15px">请搜索想要合并的产品</li>');
				$("#landUlMerged").find(".list").html('');

//				$("#searchResult").children().hide();
			},
			onAfterOpen : function(){
				self.queryMerged(self.lid,self.salerid);
			},
			onBeforeClose : function(){},
			onAfterClose : function(){},
			content : $("#landDialogTpl").html(),
			EVENTS : {
				"click" : {
					"#landSearchBtn" : function(that,e){
						var tarInp = $("#landSearchInp");
						var ltitle = tarInp.val();
						var lid = self.lid;
						var salerid = self.salerid;
						self.queryLand(lid,salerid,ltitle,2);
					},
					"#mergeBtn" : function(that,e){
						self.merge();
					}
				},
				"keydown" : {
					"#landSearchInp" : function(that,e){
						var keyCode = e.keyCode;
						if(keyCode!=13) return false;
						var tarInp = $("#landSearchInp");
						var ltitle = tarInp.val();
						var lid = self.lid;
						var salerid = self.salerid;
						self.queryLand(lid,salerid,ltitle,2);
					}
				}
			}
		});
	},
	/**
	 *
	 * @param lid
	 * @param salerid
	 * @param title
	 * @param state  state=1 => 查询已合并的    state=2 => 查询
	 */
	queryLand : function(lid,salerid,title,state){
		var that = this;
		dataCore.queryLand(lid,salerid,title,state,{
			loading : function(){
				$("#landUl").find(".list").html('<li class="empty" style="height:150px; line-height:150px; padding-left:15px">正在搜索...</li>');
			},
			removeLoading : function(){
				$("#landUl").find(".list").html('');
			},
			tiemout : function(){ $("#landUl").find(".list").html('<li class="empty" style="height:150px; line-height:150px; padding-left:15px">请求超时，请稍后重试...</li>');},
			serverError : function(){ $("#landUl").find(".list").html('<li class="empty" style="height:150px; line-height:150px; padding-left:15px">请求出错，请稍后重试...</li>');},
			success : function(res){
				var list = res.list;
				var html = "";
				var tpl = $("#itemTpl").html();
				for(var i in list){
					html += that.parseTemplate(tpl,list[i]);
				}
				html = html ? html : '<li class="empty" style="height:150px; line-height:150px; padding-left:15px">没有相应产品...</li>';
				$("#landUl").show().find(".list").html(html);
			},
			fail : function(res){}
		})
	},
	queryMerged : function(lid,salerid){
		var that = this;
		dataCore.queryLand(lid,salerid,"",1,{
			loading : function(){
				$("#landUlMerged").find(".list").html('<li style="height:150px; line-height:150px; padding-left:15px;">正在加载...</li>')
			},
			removeLoading : function(){
				$("#landUlMerged").find(".list").html('')
			},
			tiemout : function(){ $("#landUlMerged").find(".list").html('<li style="height:150px; line-height:150px; padding-left:15px;">请求超时...</li>')},
			serverError : function(){ $("#landUlMerged").find(".list").html('<li style="height:150px; line-height:150px; padding-left:15px;">请求出错...</li>')},
			success : function(res){
				var list_merged = res.list;
				var merged = "";
				var tpl = $("#itemTpl").html();
				for(var i in list_merged){
					merged += that.parseTemplate(tpl,list_merged[i]);
				}
				merged = merged ? merged : '<li class="empty" style="height:150px; line-height:150px; padding-left:15px">还没有已合并产品...</li>';
				$("#landUlMerged").show().find(".list").html(merged);
			},
			fail : function(res){}
		})
	},
	merge : function(){
		var that = this;
		var mergeBtn = $("#mergeBtn");
		var main_id = this.lid;
		var merge_id = [];
		if(mergeBtn.hasClass("loading")) return false;
		$("#landUl").find("input:checkbox[checked]").each(function(){
			merge_id.push($(this).attr("data-lid"));
		});
		dataCore.merge({
			main_id : main_id,
			merge_id : merge_id,
			loading : function(){
				mergeBtn.addClass("loading").text("正在合并...");
			},
			removeLoading : function(){
				mergeBtn.removeClass("loading").text("合并");
			},
			tiemout : function(){ alert("请求超时，请稍后重试")},
			serverError : function(){ alert("请求出错，请稍后重试")},
			success : function(res){
				that.dialog.close({
					onAfterClose : function(){
						PFT_GLOBAL.U.Alert("success",'<p style="width:180px">合并成功</p>');
					}
				});
			},
			fail : function(res){
				var msg = res.msg || "合并失败，请稍后重试";
				alert(msg);
			}
		})
	},
	open : function(opt){
		opt = opt || {};
		this.dialog.open(opt);
	}
});